<?php

function e($msg,$gourl='',$back=1)
{
	Message($msg,$back,$back);
}
function s($msg,$gourl='',$back=0)
{
	Message($msg,$gourl,$back);
}
function stop($msg,$gourl='',$back=3)
{
	Message($msg,$gourl,$back);
}
function Message($msg,$gourl,$back)
{
	
	$jumptime = 2000;
	$htmlhead = '<html><head><title>提示信息</title><meta http-equiv="content-Type" content="text/html charset=gb2312" /><META HTTP-EQUIV="pragma" CONTENT="no-cache"><META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">';
	$htmlhead .= '<base target="_self"/></head><body leftmargin="0" topmargin="0" bgcolor="#FFFFFF"><center><script>';
	$htmlfoot  = '</script></center></body></html>';
	if(1==$back){
		$jumptime = 2000;
		$gourl = "javascript:history.go(-1);";
	}

	$func = "var pgo=0;function JumpUrl(){if(pgo==0) location='$gourl';pgo = 1;}";
	$rmsg = $func;
	$rmsg .= 'document.write(\'<div style="border:1px solid #FF8C00;width:550px;margin-top:25px;padding:1px;">\');';
	$rmsg .= 'document.write(\'<div style="height:20px;font-size:16px;font-weight:bold;BACKGROUND:#FF8C00;BORDER-BOTTOM:#FF8C00 2px solid;color:#FFFFFF;vertical-align:middle;padding:5px;">- 提示信息 -</div>\');';
	$rmsg .= 'document.write(\'<div style="height:100px;font-size:14px;"><br/>\');';

	$rmsg .= "document.write(\"$msg\");";
	if($back!=3)
	{
		$rmsg .= 'document.write(\'<br/><br/><a href="'.$gourl.'" style="font-size:12px;color:#336699;"><u>系统将在'.($jumptime/1000).'秒钟后自动跳转,如果你的浏览器没反应,请点击这里</u></a><br/><br/><div>\');';
	}
	$rmsg .= 'document.write("</div>");';
	if($back!=3)
	{
		$rmsg .= "setTimeout('JumpUrl()',$jumptime);";
	}
	$msg  = $htmlhead . $rmsg . $htmlfoot;
	echo $msg;
	
	exit();
}
function table($var)
{
	return tablepre.$var;
}
function A($var,$exit=0)
{
	if(is_array($var))
	{
		echo "<pre>";
		print_r($var);
	}else
	{
		echo $var;
	}
	if($exit)
	{
		exit();
	}
}


function sgmdate($dateformat, $timestamp='', $format=0) {
	$mtime = explode(' ', microtime());
    $ftime = $mtime[1];//获取当前时间戳
	$timeoffset = 0;//时差
	$result = '';
	$dateformat = strtotime($dateformat);
	if(empty($timestamp)) 
	{
		$timestamp = $ftime;
	}
	if($format) {
		$time = $ftime - $timestamp;
		
		if($time > 24*3600*30) {
			$result = gmdate($dateformat, $timestamp + $timeoffset * 3600);
		} elseif($time > 24*3600)
		{
			$result = intval($time/(3600*24))."天前";
			
		}elseif ($time > 3600) {
			$result = intval($time/3600)."小时前";
		} elseif ($time > 60) {
			$result = intval($time/60)."分钟前";
		} elseif ($time > 0) {
			$result = $time."秒前";
		} else {
			$result = "刚刚";
		}
	} else {
		$result = gmdate($dateformat, $timestamp + $timeoffset * 3600);
	}
	return $result;
}
/*
	smarty时间格式化
*/
function insert_get_current_time($params,&$smarty)
{
	return sgmdate('Y-m-d H:i',strtotime($params['ftime']),1);
}

function get_team_orders($db,$p)
{
	
	if($p["is_team"]==1)
	{
		$query = $db->query("select * from ".table("order_team")." where orders_id=".$p["foodid"]);
		$rs = $query->row_array();
		
		return "<a href='jvascript:' title='订餐人数:".$rs["orders_person"]."&#13;&#10;订餐总价:".$rs["price"]."&#13;&#10;人均:".$rs["per_price"]."'>团体订餐</a>";
	}else
	{
		return $p["foodname"];
	}
}
function js($url="")
{
	$js = "<script language='javascript'>";
	if($url=="")
	{
		$js.="location.href='".templateurl."'";
	}else
	{
		$js.="location.href='".$url."'";
	}
	$js.="</script>";
	echo $js;
}
function getcon($varName)
{
    switch($res = get_cfg_var($varName))
    {
        case 0:
        return 'NO';
        break;
        case 1:
        return 'YES';
        break;
        default:
        return $res;
        break;
    }
     
}
function getfoodlist(&$db,$eatid)
{
	$result = array();
	$query = $db->query("select a.foodname,a.foodid,a.price,a.sortid,s.names from ".table("food")." as a left join ".table("foodsort")." as s on a.sortid=s.sortid where a.eatid=".$eatid." order by s.sortid asc");
	if($query->num_rows()>0)
	{
		$rs = $query->result_array();
		
		foreach ($rs as $v)
		{
			$result[$v["names"]][] = array(
								 $v["foodid"],$v["foodname"],$v["price"]
								);
		}
		
		return $result;
	}
}
function geteatinfo(&$db,$eatid)
{
	$query = $db->query("select eatname,sendstart,sendcost,foodtime,tel,remark from ".table("eatery")." where eateryid=".$eatid);
	$rs = $query->row_array();
	return $rs;
}


function checksession($session,$name="memberdata")
{
	$session_id = $session->userdata($name);
    if(!$session_id)
    {
    	if($name!="memberdata")
    	{
    		//echo "<script type='text/javascript'>self.parent.LoginWindow();</script>";
    		
    		e("请先登陆");
    	}
    	
    }
    return $session_id;
}

function checkprice($sessiondata,$userid,&$db)
{
	if(count($sessiondata)>0)
	{
		$foodids = array_keys($sessiondata);
		$query = $db->query("select price,foodid from ".table("food")." where foodid in(".implode(",",$foodids).")");
		$rs = $query->result_array();
		$price = array();
		if(is_array($rs))
		{
			foreach ($rs as $v)
			{
				$price[$v["foodid"]] = $v["price"];
			}
		}
		$num = "";
		foreach ($sessiondata as $key=>$d)
		{
			$num+=$d*$price[$key];
		}
		$query = $db->query("select balance from ".table("users")." where userid=".$userid);
		$rs = $query->row_array();
		if($num>$rs["balance"])
		{
			return false;
		}else
		{
			return true;
		}
	}else
	{
		return true;
	}
	
	
}
function insert_format_orders_isdo($params,&$smarty)
{
	$result = "<font color='red'>未处理</font>";
	if($params["isdo"]==1)
	{
		$result = "已处理";
		
	}
	return $result;
}
function checkcookie($cookie,$session,$db)
{
	$result = 0;
	if(!empty($cookie["userid"]))
	{
		$session_id = $session->userdata("memberdata");
		if(!$session_id)
		{
				$querycom = $db->query("select u.userid,u.areaid,c.endtime,c.companyid from ".table("users")." as u left join ".table("company")." as c on u.companyid=c.companyid where userid=".$cookie["userid"]);
    			$rs = $querycom->row_array();
    			$data = array(
    						"userid"=>$rs["userid"],
    						"companyid"=>$rs["companyid"],
    						"areaid"=>$rs["areaid"],
    						"cartend"=>$rs["endtime"]
    			           );
    			$session->set_userdata("memberdata",$data);
    			$result = 2;
		}
	}else
	{
		$session_id = $session->userdata("memberdata");
		if(!$session_id)
		{
			$result = 1;
		}
	}
	return $result;
	
}
function dotime($endtime)
{
	$end = date("Y-m-d")." ".$endtime.":00";
	
	if(strtotime($end)>=time())
	{
		return true;
	}else
	{
		return false;
	}
}
 //写入文件
function swritefile($filename, $writetext, $openmod='wb') {
	if(@$fp = fopen($filename, $openmod)) {
		flock($fp, 2);
		//fwrite($fp,"\xEF\xBB\xBF");//设置成utf-8的文件
		fwrite($fp, $writetext);
		fclose($fp);
		return true;
	}
}
function getuserinfo(&$db,$uid,$field)
{
	$query = $db->query("select ".implode(",",$field)." from ".table("users")." where userid=".$uid);
	$rs = $query->row_array();
	return $rs;
}
function output(&$ci,$info)
{
	$ci->tp->assign("info",$info);
    $ci->tp->show("output");
}
?>